using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.AcmsControls
{
    [Serializable]
    public class ImageContentControlProperties : BaseContentControlProperties, System.Xml.Serialization.IXmlSerializable
    {
        private int resourceId;
        public int ResourceId
        {
            get { return resourceId;}
            set { resourceId = value; }
        }

        public bool HasResource()
        {
            return resourceId > 0;
        }

        private string altText;
        public string AltText
        {
            get { return altText; }
            set { altText = value; }
        }

        private int width;

        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        private int height;

        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    ReadValue(reader);
                }
                reader.MoveToElement();
            }
            while (reader.Read())
            {
                if(reader.AttributeCount > 0)
                {
                    while(reader.MoveToNextAttribute())
                    {
                        ReadValue(reader);
                    }
                    reader.MoveToElement();
                }
                ReadValue(reader);
            }
        }

        private void ReadValue(System.Xml.XmlReader reader)
        {
            if (reader.Name.ToLower() == "resourceid" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    resourceId = reader.ReadContentAsInt();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "width" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    width = reader.ReadContentAsInt();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "height" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    height = reader.ReadContentAsInt();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "alttext" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    altText = reader.ReadContentAsString();
                }
                catch
                {
                }
            }
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {

            writer.WriteStartElement("content");

            writer.WriteAttributeString("resourceId", resourceId.ToString());
            writer.WriteAttributeString("width", width.ToString());
            writer.WriteAttributeString("height", height.ToString());
            
            writer.WriteStartElement("alttext");
            writer.WriteAttributeString("search", "true");
            writer.WriteValue(this.AltText);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }

        #endregion
    }
}
